﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC_Project.Model;
using MVC_Project.Bll.Systems;
using System.Dynamic;
using Newtonsoft.Json;

namespace MVC_Project.UI.Areas.System.Controllers
{
    [PSSAuthorize]
    public class UsersController : Controller
    {
        // GET: Users
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetUsersListPage(int page, int limit,string name) {

            UsersBll bll = new UsersBll();

            int count = 0;
            var users = bll.GetUsersPage(page, limit, name, out count);

            // 将数据模型 转换成 dto 模型
            List<Models.Users> list = new List<Models.Users>();    
            foreach (var u in users)
            {
                list.Add(new Models.Users()
                {
                    userID = u.userID,
                    name = u.name,
                    imgID = u.imgID,
                    path = u.Images.path,
                    lastTime = u.lastTime,
                    loginName = u.loginName,
                    sex = u.sex,
                    phone = u.phone,
                    status = u.status,  
                    loginPwd = u.loginPwd,
                    roleID = u.roleID,
                    roleName = u.Roles.roleName,

                });
            }

            dynamic d = new ExpandoObject();
            d.code = 0;
            d.msg = "ok";
            d.count = count;
            d.data = list;

            string res = JsonConvert.SerializeObject(d);

            return Content(res,"application/json");

        }


        public ActionResult Create()
        {
            DeptsBll dbll = new DeptsBll();
            var depts = dbll.QueryList();

            RolesBll rbll = new RolesBll();
            var roles = rbll.QueryList();


            ViewBag.depts = depts;
            ViewBag.roles = roles;

            return View();
        }

        [HttpPost]
        public ActionResult Create(Users users)
        {
            UsersBll bll = new UsersBll();

            users.lastTime = DateTime.Now;
            users.imgID = 1;
            users.loginPwd = MD5Helper.Md5(users.loginPwd);

            var num = bll.Add(users);
            dynamic d = new ExpandoObject();
            if (num>0)
            {
                d.code = 1;
                d.msg = "添加成功！";
               
            }
            else
            {
                d.code = -1;
                d.msg = "添加失败！";
                
            }
            return Content(JsonConvert.SerializeObject(d), "application/json");
        }

        public ActionResult Edit(int id)
        {
            DeptsBll dbll = new DeptsBll();
            var depts = dbll.QueryList();

            RolesBll rbll = new RolesBll();
            var roles = rbll.QueryList();

            ViewBag.depts = depts;
            ViewBag.roles = roles;

            UsersBll bll = new UsersBll();
            var user = bll.GetUsersById(id);


            return View(user);
        }

        [HttpPost]
        public ActionResult Edit(Users users)
        {
            UsersBll bll = new UsersBll();

            users.lastTime = DateTime.Now;

            var num = bll.Update(users);
            dynamic d = new ExpandoObject();
            if (num > 0)
            {
                d.code = 1;
                d.msg = "修改成功！";

            }
            else
            {
                d.code = -1;
                d.msg = "修改失败！";

            }
            return Content(JsonConvert.SerializeObject(d), "application/json");
        }

        [HttpPost]
        public ActionResult DeleteBatch(string[] ids) {
            UsersBll bll = new UsersBll();

            var users = bll.Query().Where(u => ids.Contains(u.userID.ToString())).ToList();
            var num = bll.Delete(users);
            dynamic d = new ExpandoObject();
            if (num > 0)
            {
                d.code = 1;
                d.msg = "删除成功！";

            }
            else
            {
                d.code = -1;
                d.msg = "删除失败！";

            }
            return Content(JsonConvert.SerializeObject(d), "application/json");
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            UsersBll bll = new UsersBll();

            var user = bll.Query().Where(u => u.userID == id).FirstOrDefault();
            var num = bll.Delete(user);
            dynamic d = new ExpandoObject();
            if (num > 0)
            {
                d.code = 1;
                d.msg = "删除成功！";

            }
            else
            {
                d.code = -1;
                d.msg = "删除失败！";

            }
            return Content(JsonConvert.SerializeObject(d), "application/json");
        }
    }
}